void init_radio()
{
	rc_1.radio_min = CH1_MIN;
	rc_1.radio_max = CH1_MAX;

	rc_2.radio_min = CH2_MIN;
	rc_2.radio_max = CH2_MAX;

	rc_3.radio_min = CH3_MIN;
	rc_3.radio_max = CH3_MAX;

	rc_4.radio_min = CH4_MIN;
	rc_4.radio_max = CH4_MAX;

	rc_1.set_reverse(REVERSE_ROLL);
	rc_2.set_reverse(REVERSE_PITCH);
	rc_4.set_reverse(REVERSE_RUDDER);

	rc_1.set_angle(4500);
	rc_2.set_angle(4500);
	rc_3.set_range(0,1000);
	rc_4.set_angle(4500);

	rc_1.dead_zone = 85;
	rc_2.dead_zone = 85;
	rc_3.dead_zone = 40;
	rc_4.dead_zone = 85;

	rc.init();		// APM Radio initialization
	rc.output_ch_pwm(CH_1, 	rc_1.radio_trim);					// Initialization of servo outputs
	rc.output_ch_pwm(CH_2, 	rc_2.radio_trim);
	rc.output_ch_pwm(CH_3, 	rc_3.radio_min);
	rc.output_ch_pwm(CH_4, 	rc_4.radio_trim);
}

void read_radio()
{
	#if MIXING_MODE == 0
		rc_1.set_pwm(rc.input_ch(CH_ROLL));
		rc_2.set_pwm(rc.input_ch(CH_PITCH));
	#else
		ch1_temp = rc.input_ch(CH_ROLL);
		ch2_temp = rc.input_ch(CH_PITCH);
		rc_1.set_pwm(reverse_elevons * (reverse_ch2_elevon * int(ch2_temp - elevon2_trim) - reverse_ch1_elevon * int(ch1_temp - elevon1_trim)) / 2 + 1500);
		rc_2.set_pwm((reverse_ch2_elevon * int(ch2_temp - elevon2_trim) + reverse_ch1_elevon * int(ch1_temp - elevon1_trim)) / 2 + 1500);
	#endif

	rc_3.set_pwm(rc.input_ch(CH_3));
	rc_4.set_pwm(rc.input_ch(CH_4));

	//  TO DO  - go through and patch throttle reverse for RC_Channel library compatibility
	#if THROTTLE_REVERSE == 1
		radio_in[CH_THROTTLE] = radio_max(CH_THROTTLE) + radio_min(CH_THROTTLE) - radio_in[CH_THROTTLE];
	#endif

	#if THROTTLE_FAILSAFE == 1
		throttle_failsafe(rc_3.radio_in);
	#endif

	// throttle passthrough
	rc_3.servo_out = rc_3.control_in;

//Radio C_IN 	1: 0	2: 0	3: 993	4: 0
//Radio S_OUT 	1: 0	2: 0	3: 60	4: 0
//Radio R_OUT 	1: 1523	2: 1522	3: 1150	4: 1517

	/*
	if (rc_3.servo_out > 50) {
		if(airspeed_enabled == true) {
			airspeed_nudge = (g.flybywire_airspeed_max * 100 - g.airspeed_cruise) * ((rc_3.norm_input()-0.5) / 0.5);
        } else {
			throttle_nudge = (g.throttle_max - g.throttle_cruise) * ((rc_3.norm_input()-0.5) / 0.5);
		}
	} else {
		airspeed_nudge = 0;
		throttle_nudge = 0;
	}
	*/
	/*
	Serial.printf_P(PSTR("OUT 1: %d\t2: %d\t3: %d\t4: %d \n"),
				g.rc_1.control_in,
				g.rc_2.control_in,
				g.rc_3.control_in,
				g.rc_4.control_in);
	*/
}


byte failsafeCounter = 0;		// we wait a second to take over the throttle and send the plane circling

#if THROTTLE_FAILSAFE == 1
void throttle_failsafe(uint16_t pwm)
{
	if(throttle_fs_enabled == 0)
		return;

	//check for failsafe and debounce funky reads
	// ------------------------------------------
	if (pwm < THROTTLE_FS_VALUE){
		// we detect a failsafe from radio
		// throttle has dropped below the mark
		failsafeCounter++;
		if (failsafeCounter == 9){
			Serial.printf_P(PSTR("FS ON\n"));

			//SendDebug("MSG FS ON ");
			//SendDebugln(pwm, DEC);
		}else if(failsafeCounter == 10) {
			ch3_failsafe = true;
			Serial.printf_P(PSTR("FS OFF\n"));
			//set_failsafe(true);
			//failsafeCounter = 10;
		}else if (failsafeCounter > 10){
			failsafeCounter = 11;
		}

	}else if(failsafeCounter > 0){
		// we are no longer in failsafe condition
		// but we need to recover quickly
		failsafeCounter--;
		if (failsafeCounter > 3){
			failsafeCounter = 3;
		}
		if (failsafeCounter == 1){
			//SendDebug("MSG FS OFF ");
			//SendDebugln(pwm, DEC);
		}else if(failsafeCounter == 0) {
			ch3_failsafe = false;
			//set_failsafe(false);
			//failsafeCounter = -1;
		}else if (failsafeCounter <0){
			failsafeCounter = -1;
		}
	}
}
#endif

void trim_control_surfaces()
{
	read_radio();
	// Store control surface trim values
	// ---------------------------------
	#if MIXING_MODE == 0
		rc_1.radio_trim 	= rc_1.radio_in;
		rc_2.radio_trim 	= rc_2.radio_in;
		rc_4.radio_trim 	= rc_4.radio_in;
	#else
		elevon1_trim = ch1_temp;
		elevon2_trim = ch2_temp;
		//Recompute values here using new values for elevon1_trim and elevon2_trim
		//We cannot use radio_in[CH_ROLL] and radio_in[CH_PITCH] values from read_radio() because the elevon trim values have changed
		rc_1.radio_trim 	= 1500;
		rc_2.radio_trim 	= 1500;
	#endif

	// save to eeprom
	rc_1.save_eeprom();
	rc_2.save_eeprom();
	//rc_3.save_eeprom();
	rc_4.save_eeprom();
}



